package shuzi.adapter;

import java.util.List;

import shuzi.data.Database;
import shuzi.model.BrandPhone;
import shuzi.util.Dialog;
import shuzi.util.TextViewToDBC;
import shuzi.widget.AlwaysMarqueeTextView;
import shuzi.widget.BrandImageView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView.ScaleType;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shuzi.offer.R;

public class BrandPhoneAdapter extends BaseAdapter {

	private final LayoutInflater mInflater;
	private List<BrandPhone> list;
	private Context context;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = null;

	public BrandPhoneAdapter(Context context, List<BrandPhone> list) {
		this.mInflater = LayoutInflater.from(context);
		this.list = list;
		this.context = context;
		init();
	}

	private void init() {
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.phone_defult)
				.showImageForEmptyUri(R.drawable.phone_defult)
				.showImageOnFail(R.drawable.phone_defult).cacheInMemory().cacheOnDisc()
				// .displayer(new RoundedBitmapDisplayer(10))
				.build();
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return this.list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.list.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public final class ViewHolder {
		public BrandImageView img;
		public AlwaysMarqueeTextView name;
		public AlwaysMarqueeTextView network;
		public AlwaysMarqueeTextView handware_info;
		public AlwaysMarqueeTextView screen_size;
		public AlwaysMarqueeTextView os;
		public AlwaysMarqueeTextView price;
		public CheckBox check;
		public RadioGroup group;
		public RadioButton whiteButton;
		public RadioButton blackButton;
		public RadioButton otherButton;
		private final View baseView;

		public ViewHolder(View baseView) {
			this.baseView = baseView;
		}

		public RadioButton getBlackButton() {
			return (RadioButton) baseView.findViewById(R.id.blackButoon);
		}

		public void setBlackButton(RadioButton blackButton) {
			this.blackButton = blackButton;
		}

		public RadioButton getOtherButton() {
			return (RadioButton) baseView.findViewById(R.id.otheerButoon);
		}

		public void setOtherButton(RadioButton otherButton) {
			this.otherButton = otherButton;
		}

		public RadioButton getWhiteButton() {
			return (RadioButton) baseView.findViewById(R.id.whitleButoon);
		}

		public void setWhiteButton(RadioButton whiteButton) {
			this.whiteButton = whiteButton;
		}

		public RadioGroup getGroup() {
			return (RadioGroup) baseView.findViewById(R.id.radioGroup);
		}

		public void setGroup(RadioGroup group) {
			this.group = group;
		}

		public BrandImageView getImg() {
			return (BrandImageView) baseView.findViewById(R.id.infoImage);
		}

		public void setImg(BrandImageView img) {
			this.img = img;
		}

		public AlwaysMarqueeTextView getName() {
			return (AlwaysMarqueeTextView) baseView.findViewById(R.id.name);
		}

		public void setName(AlwaysMarqueeTextView name) {
			this.name = name;
		}

		public AlwaysMarqueeTextView getNetwork() {
			return (AlwaysMarqueeTextView) baseView.findViewById(R.id.network);
		}

		public void setNetwork(AlwaysMarqueeTextView network) {
			this.network = network;
		}

		public AlwaysMarqueeTextView getHandware_info() {
			return (AlwaysMarqueeTextView) baseView
					.findViewById(R.id.handware_info);
		}

		public void setHandware_info(AlwaysMarqueeTextView handware_info) {
			this.handware_info = handware_info;
		}

		public AlwaysMarqueeTextView getScreen_size() {
			return (AlwaysMarqueeTextView) baseView
					.findViewById(R.id.screen_size);
		}

		public void setScreen_size(AlwaysMarqueeTextView screen_size) {
			this.screen_size = screen_size;
		}

		public AlwaysMarqueeTextView getOs() {
			return (AlwaysMarqueeTextView) baseView.findViewById(R.id.os);
		}

		public void setOs(AlwaysMarqueeTextView os) {
			this.os = os;
		}

		public AlwaysMarqueeTextView getPrice() {
			return (AlwaysMarqueeTextView) baseView.findViewById(R.id.price);
		}

		public void setPrice(AlwaysMarqueeTextView price) {
			this.price = price;
		}

		public CheckBox getCheck() {
			return (CheckBox) baseView.findViewById(R.id.check);
		}

		public void setCheck(CheckBox check) {
			this.check = check;
		}

	}

	public class MyCheckOnclick implements OnClickListener {
		RadioGroup group;
		int position;
		CheckBox check;

		public MyCheckOnclick(RadioGroup group, int position, CheckBox check) {
			this.group = group;
			this.position = position;
			this.check = check;
		}

		@Override
		public void onClick(View v) {
			if (!list.get(position).isCheck()) {
				// 如果没有选择颜色，返回
				if (list.get(position).getChoiceColor() == -1) {
					check.setChecked(false);
					Dialog.choiceColorDialog(context);
					return;
				}

				String price = null;
				switch (list.get(position).getChoiceColor()) {
				case Database.WHITE:
					price = list.get(position).getWihtePrice();
					break;
				case Database.BLACK:
					price = list.get(position).getBlackPrice();
					break;

				case Database.OTHER:
					price = list.get(position).getOtherPrice();
					break;

				default:
					break;
				}

				if (price == null || price.equals("")) {
					check.setChecked(false);
					Dialog.choiceColorDialog(context);
					return;
				}

				if (!Database.yiXuanMap.containsKey(list.get(position).getId()
						+ list.get(position).getChoiceColor())) {

					Database.yiXuanMap.put(list.get(position).getId()
							+ list.get(position).getChoiceColor(),
							BrandPhone.copyItself(list.get(position)));
					list.get(position).setCheck(true);
					Log.v(Database.TAG, "添加" + list.get(position).getName());
					Toast.makeText(context,
							list.get(position).getName() + "添加到订单",
							Toast.LENGTH_SHORT).show();
				} else {// 如果存在key的话
					Database.yiXuanMap.get(
							list.get(position).getId()
									+ list.get(position).getChoiceColor())
							.setNumber(
									Database.yiXuanMap.get(
											list.get(position).getId()
													+ list.get(position)
															.getChoiceColor())
											.getNumber() + 1);
					list.get(position).setCheck(true);
					Log.v(Database.TAG, "添加" + list.get(position).getName());
					Toast.makeText(context,
							list.get(position).getName() + "添加到订单",
							Toast.LENGTH_SHORT).show();
				}
			} else {
				list.get(position).setCheck(false);
				Database.yiXuanMap.remove(list.get(position).getId()
						+ list.get(position).getChoiceColor());
				Log.v(Database.TAG, "删除" + list.get(position).getName());
				Toast.makeText(context, list.get(position).getName() + "取消订单",
						Toast.LENGTH_SHORT).show();
				group.clearCheck();
				group.setClickable(true);
			}

		}

	}

	public class ColorButtonClick implements OnClickListener {
		int color;
		RadioGroup group;
		int position;
		TextView price;
		CheckBox check;

		public ColorButtonClick(int color, RadioGroup group, int position,
				TextView price, CheckBox check) {
			this.color = color;
			this.group = group;
			this.position = position;
			this.price = price;
			this.check = check;
		}

		@Override
		public void onClick(View v) {
			check.setChecked(false);
			list.get(position).setCheck(false);
			list.get(position).setChoiceColor(color);
			group.setClickable(false);
			switch (color) {
			case Database.WHITE:
				list.get(position).setPrice(list.get(position).getWihtePrice());
				break;
			case Database.BLACK:
				list.get(position).setPrice(list.get(position).getBlackPrice());
				break;

			case Database.OTHER:
				list.get(position).setPrice(list.get(position).getOtherPrice());
				break;

			default:
				break;
			}
			price.setText("￥" + list.get(position).getPrice());

		}

	}

	public View getView(final int position, View convertView, ViewGroup parent) {

		// Log.v(Database.TAG, position+"");
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = (RelativeLayout) this.mInflater.inflate(
					R.layout.info_item, null);
			holder = new ViewHolder(convertView);
			holder.img = holder.getImg();
			holder.name = holder.getName();
			holder.os = holder.getOs();
			holder.handware_info = holder.getHandware_info();
			holder.network = holder.getNetwork();
			holder.price = holder.getPrice();
			holder.screen_size = holder.getScreen_size();
			holder.check = holder.getCheck();
			holder.group = holder.getGroup();
			holder.whiteButton = holder.getWhiteButton();
			holder.blackButton = holder.getBlackButton();
			holder.otherButton = holder.getOtherButton();

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}

		list.get(position).setPrice(list.get(position).getBlackPrice());

		holder.check.setChecked(false);
		if (!list.get(position).isCheck()) {
			list.get(position).setChoiceColor(-1);
		}

		holder.group.clearCheck();
		/*
		 * for (Object m : Database.yiXuanMap.keySet()) { if
		 * (Database.yiXuanMap.get(m).getId()
		 * .equals(list.get(position).getId())) { holder.check.setChecked(true);
		 * list.get(position).setCheck(true); Log.v(Database.TAG,
		 * Database.yiXuanMap.get(m).getName() +
		 * Database.yiXuanMap.get(m).getChoiceColor() + ""); switch
		 * (Database.yiXuanMap.get(m).getChoiceColor()) { case Database.WHITE:
		 * holder.whiteButton.setChecked(true); list.get(position).setPrice(
		 * list.get(position).getWihtePrice()); break; case Database.BLACK:
		 * holder.blackButton.setChecked(true); list.get(position).setPrice(
		 * list.get(position).getBlackPrice());
		 * 
		 * break; case Database.OTHER: list.get(position).setPrice(
		 * list.get(position).getOtherPrice());
		 * holder.otherButton.setChecked(true);
		 * 
		 * break;
		 * 
		 * default: // holder.whiteButton.setChecked(true); break; } } }
		 */
		holder.check.setOnClickListener(new MyCheckOnclick(holder.group,
				position, holder.check));

		holder.whiteButton.setOnClickListener(new ColorButtonClick(
				Database.WHITE, holder.group, position, holder.price,
				holder.check));

		holder.blackButton.setOnClickListener(new ColorButtonClick(
				Database.BLACK, holder.group, position, holder.price,
				holder.check));
		holder.otherButton.setOnClickListener(new ColorButtonClick(
				Database.OTHER, holder.group, position, holder.price,
				holder.check));

		// holder.img.setImageDrawable(new ColorDrawable(color.lightblue));
		holder.name.setText(list.get(position).getName());
		holder.handware_info.setText("硬件:"
				+ TextViewToDBC.ToDBC(list.get(position).getHardwareInfo()));
		holder.network.setText("制式:"
				+ TextViewToDBC.ToDBC(list.get(position).getNetwork()));
		holder.price.setText("￥" + list.get(position).getPrice());
		holder.os.setText("系统:" + list.get(position).getOs());
		holder.screen_size.setText("尺寸:" + list.get(position).getScreenSize());
		Log.i(Database.TAG,
				list.get(position).getName() + "图片地址为："
						+ list.get(position).getPicture());
	/*	holder.img.setImageViewBg(list.get(position).getPicture(),
				list.get(position).getName(), position, Database.PHONE_INFO);*/
		holder.img.setScaleType(ScaleType.FIT_XY);
		imageLoader.displayImage(list.get(position).getPicture(), holder.img, options);
		
		return convertView;
	}

}
